import numpy as np
import clr
import os
import matplotlib.pyplot as plt
import time

clr.AddReference('Spectrum')
from System.Collections.Generic import List
from System import Single
from Spectrum import *


def readAccelData(file):
    data = np.loadtxt(file, skiprows=13)
    with open(file, 'r') as csv_file:
        for i in range(10):
            row = csv_file.readline()
            if i == 9:
                dt = float(row.split()[1])
            if i == 5:
                pga = float(row.split()[1])
    return data, dt, pga


sm = SpectrumMethod()

# 阻尼比
dr = 0.05
# 反应谱的周期长度
periodLength = 10.0
# 周期计算间隔
deltaPeriod = 0.01
# 需要计算的地震持时，如果为-1则计算整条地震动，
duration = -1
# 是否输出伪反应谱i
isPeseudo = True

file = r'F:\刘中欢\高层所\同门工程\吴梓楠\反应谱计算速度对比\NGA_no_175_H-E12140.txt'



accelData, dt, pga = readAccelData(file)

# 积分步长
integarteDt = dt

# 对加速度时程进行放缩
# scale = pga / 35.0
# accelData /= scale

# 输出C#所需要的加速度时程数组
accelList = List[Single]()
for each in accelData:
    accelList.Add(each)

start=time.time()
for i in range(1,2):
    result = sm.GetResults(accelList, dt, dr, periodLength, deltaPeriod, integarteDt, duration, isPeseudo)

end=time.time()
print(end-start)

# response spectrum value 反应谱幅值
rsv = np.array([each.Afa for each in result])

# 放大系数谱
# beta = rsv / 35.0

# 对应周期
period = [each.Period for each in result]

plt.plot(period,rsv)
plt.xlim(xmin=0)
plt.ylim(ymin=0)
plt.title('反应谱') #标题
plt.rcParams['font.sans-serif']=['SimHei']
plt.rcParams['axes.unicode_minus']=False
plt.xlabel(r'T/s')
plt.ylabel(r'cm/s^2')
plt.show()